#!/usr/bin/env python

# Copyright 2023 CMU, Facebook, LANL, MIT, NVIDIA, and Stanford (alphabetical)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# IMPORTANT:
#   * legion_cffi.py.in is used as an input to string.format()
#   * legion_cffi.py is a generated file and should not be modified by hand

from __future__ import absolute_import, division, print_function, unicode_literals

import cffi, os, sys

from flexflow.config import flexflow_python_interpreter

header = {header}
libname = {libname}

ffi = cffi.FFI()
ffi.cdef(header)

flexflow_lib = None
if flexflow_python_interpreter() == "native":
    flexflow_lib = libname
    if "PYTHONPATH" in os.environ:
        python_paths = os.environ["PYTHONPATH"].split(":")
        for p in python_paths:
            flexflow_lib_p = os.path.join(p, flexflow_lib)
            if os.path.exists(flexflow_lib_p):
                flexflow_lib = flexflow_lib_p
                break
    else:
        # When installing FlexFlow with pip, the library files are installed within
        # the pip package folder, instead of at /usr/local/lib
        ff_lib_path = os.path.join(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "lib"
        )
        ld_lib_path = os.environ.get("LD_LIBRARY_PATH") or ""
        # If the library exists at the ff_lib_path, rerun with the ff_lib_path in the LD_LIBRARY_PATH
        if os.path.exists(
            os.path.join(ff_lib_path, libname)
        ) and ff_lib_path not in ld_lib_path.split(":"):
            os.environ["LD_LIBRARY_PATH"] = ff_lib_path + ":" + ld_lib_path
            os.execv(sys.executable, ["python"] + sys.argv)
ffc = ffi.dlopen(flexflow_lib)
